﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ControlPoint : MonoBehaviour {

	private GameObject parent;

	private Vector3 screenPoint;
	private Vector3 offset;

	public int index = 0;

	// Use this for initialization
	void Start () {
		parent = this.transform.parent.gameObject;
	}
	
	// Update is called once per frame
	void Update () {
		var spline = parent.GetComponent<SplineMesh> ();
		if (spline) {
			spline.setControlPointVector (transform.position, index);
		}
	}

	void OnMouseDown()
	{
		screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
		offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
	}

	void OnMouseDrag()
	{
		Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

		Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
		transform.position = curPosition;
	}
}
